<?php

/**
 * Controller_manage
 * 各个管理
 */

class Controller_manage extends MyApp {

	public function __construct()
	{

		parent::__construct();

		$this->model->manage  = YOU::loadModel('manage');
		$this->model->user    = YOU::loadModel('user');
		$this->model->file    = YOU::loadModel('file');
		$this->model->class   = YOU::loadModel('class');
		$this->model->grade   = YOU::loadModel('grade');
	}

	/** 首页 **/
	public function actionIndex()
	{

	}
	/** 二级联动 **/
	public function actionListJson ()
	{
		$grade_id = (int)$this->get->grade_id;
		$class_lists 	= $this->model->class->getinfo($grade_id);

		if(!empty($class_lists)){
			echo json_encode($class_lists);
		}else{
			exit("0");
		}
	}

	/** --------------------------- 各个管理 --------------------------------- **/
	/** 列表页 **/
	public function actionManageClassifyList ()
	{
		// 查询条件
		$class_id		= $this->get->class_id;
		$manage_title	= $this->get->manage_title;
		$staff_name		= $this->get->staff_name;
		$from			= $this->get->from;
		$to				= $this->get->to;
		$manage_type	= (int)$this->get->manage_type;
      

		$where = " WHERE 1=1 ";

		if(!empty($manage_title)){
			//模糊搜索
			$where .= " AND manage_title LIKE '%{$manage_title}%' ";
			$this->tpl->assign("manage_title", $manage_title);
		}

		if(!empty($staff_name)){

			$where .= " AND user_id IN(SELECT `user_id` FROM `tbl_staff` as a, `tbl_user` as b 
			WHERE a.`staff_name` LIKE '%{$staff_name}%' and  b.type_id = a.staff_id and b.user_type = 'staff' )";
			$this->tpl->assign("staff_name", $staff_name);
		}

		if(!empty($from)){

			$where .= " AND create_time >= '{$from}' ";
			$this->tpl->assign("from", $from);
		}

		if(!empty($to)){

			$where .= " AND create_time <= '{$to}+1' ";
			$this->tpl->assign("to", $to);
		}

		// 专项检查搜索（特有）
		if(!empty($class_id)){
			//print_R($class_id);
			$where .= " AND class_id = '{$class_id}' ";
			$this->tpl->assign("class_id", $class_id);
		}

		// 各个管理进入渠道
		if(!empty($manage_type)){

			$where .= " AND manage_type='{$manage_type}' ";

		}

		// 专项检查
		if($manage_type == 27){
			// 班级
			$class_lists 	= $this->model->class->getAll();
		
		}
		// 各管理的名称
		
		$manage_name = $this->manage_name[$manage_type];
		
		// 分页
		YOU::loadLib('page', "", '0');
		$tmp = $this->db->fetchRow("SELECT COUNT(1) AS count FROM `tbl_manage` {$where}");

		$count = $tmp['count'];
		$page_total = ceil($count/PAGE_LIMIT);
		// 当前页
		$p = intval($_GET['p']);
		if ($p == 0){
			$p  = 1;
		}
		if ($p > 1){
			$start = PAGE_LIMIT * ($p-1);
		}else{
			$start = 0;
		}
		$manage_lists = $this->db->fetchAll("SELECT * FROM `tbl_manage` {$where} ORDER BY manage_id DESC LIMIT {$start}, ".PAGE_LIMIT."");

		//时间日期转换
		foreach($manage_lists as $key=>$value){
			// 时间
			$manage_lists[$key]['create_time']= date("Y-m-d", strtotime($manage_lists[$key]['create_time']));
			// 提交者
			$manage_lists[$key]['user_name'] = $this->model->user->getNameById($value['user_id']);
			// 内容解析
			$manage_lists[$key]['manage_content'] = base64_decode($manage_lists[$key]['manage_content']);
			// 控制标题字数
			$manage_lists[$key]['manage_title'] 	= mb_substr($value['manage_title'],0,25,'utf8');
			if(strlen($manage_lists[$key]['manage_title'])>25){
				$manage_lists[$key]['manage_title']=mb_substr($manage_lists[$key]['manage_title'],0,25,utf8).'...';
			}
			if($manage_type == 27){
				// 年级
				$grade_data= $this->model->grade->getinfo($value['grade_id']);
				$manage_lists[$key]['grade_name']=$grade_data['grade_name'];
				// 班级
				$class_data= $this->model->class->getinfo($value['class_id']);
				$manage_lists[$key]['class_name']=$class_data['class_name'];

			}
		}

		$page = new Page(array('total' => $count, 'perpage' => PAGE_LIMIT));
		$page->next_page   = '下一页>';
		$page->pre_page    = '<上一页';
		$page->style_path  = "static/page_style";
		$this->tpl->assign("page", $page->show(4, "yahoo"));

		$this->tpl->assign("manage_lists", $manage_lists);
		$this->tpl->assign("manage_type", $manage_type);
		$this->tpl->assign("manage_name", $manage_name);
		$this->tpl->assign("class_lists", $class_lists);
		$this->tpl->assign("manage_type", $manage_type);

		if($manage_type == 27){
			$this->tpl->display('manage/examine.list.tpl.php');
		}else{
			$this->tpl->display('manage/classify.list.tpl.php');
		}
	}


	/** 添加 **/
	public function actionManageClassifyAdd()
	{

		$manage_type	= (int)$this->get->manage_type;
		$grade_id 	    = (int)$this->get->grade_id;
		$clss_id 	    = (int)$this->get->class_id;
		// 专项检查
		if($manage_type == 27){
			// 年级
			$grade_lists 	= $this->model->grade->getAll();

		}
		if ($_POST){

			$manage_title     	    = $this->post->manage_title;
			$manage_content   		= $this->post->manage_content;
			$class_id   			= $this->post->class_id;
			$grade_id   			= $this->post->grade_id;
			$manage_content = base64_encode($manage_content);

			// 写入数据库
			$info = array(

			"class_id"  			=> $class_id,
			"grade_id"       		=> $grade_id,
			"manage_title"  	    => $manage_title,
			"manage_content"       	=> $manage_content,
			"user_id"       		=> $this->user['user_id'],
			// 各个管理的参数
			"manage_type"       	=> $manage_type,
			"status"                => 1,
			"create_time"           => date("Y-m-d H:i:s"),
			"update_time"           => "0000-00-00 00:00:00",

			);

			$this->db->do_insert("tbl_manage", $info);
			$manage_id = $this->db->insert_id();

			// 附件上传
			$file = $this->model->file->upload("manage_file", $manage_id, array("txt","ppt", "xls", "xlsx", "docs", "doc", "pdf", "jpg", "png", "gif", "zip", "rar"));

			if (!empty($file)){
				$info = array(
				"manage_file" => $file['fname'],
				);

				$this->db->do_update("tbl_manage", $info, "manage_id='{$manage_id}'");
			}
			Helper_Client::location("添加成功", "index.php?a=manage.manageclassifylist&manage_type={$manage_type}");

		}


		$this->tpl->assign("manage_type", $manage_type);
		// 专项检查 manage_type == 27
		if($manage_type == 27){
			$this->tpl->assign("grade_lists", $grade_lists);
			$this->tpl->assign("class_lists", $class_lists);
			$this->tpl->display("manage/examine.add.tpl.php");
		}else{
			$this->tpl->display("manage/classify.add.tpl.php");
		}
	}

	/** 修改 **/
	public function actionManageClassifyUpdate()
	{
		$manage_type	= (int)$this->get->manage_type;
		$manage_id		= (int)$this->get->manage_id;

		$manage_data	= $this->db->fetchRow("SELECT * FROM `tbl_manage` WHERE `manage_id`='{$manage_id}'");
	
		$manage_data['manage_content'] = base64_decode($manage_data['manage_content']);
		
		if (empty($manage_data)){
			Helper_Client::alert("文章不存在");
		}
        $uid = $this->user['user_id'];
        if($manage_data['user_id']  && ($uid !=$manage_data['user_id']) ){
            //Helper_Client::alert("对不起无此权限");
           // exit;
        }

		// 专项检查
		if($manage_type ==27){
			// 年级
			$grade_lists = $this->db->fetchAll("SELECT `grade_id`,`grade_name` FROM `tbl_grade`");
			// 班级
			$class_lists = $this->db->fetchAll("SELECT `class_id`,`class_name` FROM `tbl_class`");
		}
		if ($_POST){

			$manage_title     	    = $this->post->manage_title;
			$manage_content   		= $this->post->manage_content;
			$class_id   			= $this->post->class_id;
			$grade_id   			= $this->post->grade_id;
			$manage_content = base64_encode($manage_content);

			// 写入数据库
			$info = array(

			"class_id"  			=> $class_id,
			"grade_id"       		=> $grade_id,
			"manage_title"  	    => $manage_title,
			"manage_content"       	=> $manage_content,
			"status"                => 1,
			"create_time"           => date("Y-m-d H:i:s"),
			"update_time"           => "0000-00-00 00:00:00",

			);

			$this->db->do_update("tbl_manage", $info, "manage_id='{$manage_id}'");

			// 附件上传
			if ($this->post->manage_file_re=='1'){
				// 附件上传
				$file = $this->model->file->upload("manage_file", $manage_id, array("txt","ppt", "xls", "xlsx", "docs", "doc", "pdf", "jpg", "png", "gif", "zip", "rar"));
				
				if (!empty($file)){
					$info = array(
					"manage_file" => $file['fname'],
					);

					$this->db->do_update("tbl_manage", $info, "manage_id='{$manage_id}'");
				}
			}


			// 跳转
			Helper_Client::location("修改成功", "index.php?a=manage.manageclassifylist&manage_type={$manage_type}");

		}

		$this->tpl->assign("manage_data", $manage_data);
		if($manage_type == 27){
			$this->tpl->assign("class_lists", $class_lists);
			$this->tpl->assign("grade_lists", $grade_lists);
			$this->tpl->display("manage/examine.update.tpl.php");
		}else{
			$this->tpl->display("manage/classify.update.tpl.php");
		}
	}

	/** 详情页 **/
	public function actionManageClassifyInfoList ()
	{

		$manage_id   = (int)$this->get->manage_id;
		$manage_data = $this->model->manage->getInfo($manage_id);
		$manage_data['manage_content'] = base64_decode($manage_data['manage_content']);
		// 提交者
		$manage_data['user_name'] = $this->model->user->getNameById($manage_data['user_id']);
		if (empty($manage_data)){
			Helper_Client::alert("文章不存在");
		}

		// 添加访问
		$this->model->manage->stat($manage_id, $this->user['user_id']);


		//  读取访问记录
		$stat_lists = $this->model->manage->statLists($manage_id);


		//时间日期转换
		$manage_data['create_time']= date("Y-m-d", strtotime($manage_data['create_time']));

		$this->tpl->assign("stat_lists", $stat_lists);
		$this->tpl->assign("manage_data", $manage_data);

		$this->tpl->display('manage/classify.info.tpl.php');
	}


	/** 删除 **/
	public function actionManageClassifyDel()
	{
		$manage_type	= (int)$this->get->manage_type;
		$manage_id = $this->get->manage_id;
        $manage_data	= $this->db->fetchRow("SELECT * FROM `tbl_manage` WHERE `manage_id`='{$manage_id}'");

        if (empty($manage_data)){
            Helper_Client::alert("文章不存在");
        }
        $uid = $this->user['user_id'];
        if($manage_data['user_id']  && ($uid !=$manage_data['user_id']) ){
            Helper_Client::alert("对不起无此权限");
            exit;
        }
		$this->db->query("DELETE  FROM `tbl_manage` WHERE manage_id='{$manage_id}'");
		// 跳转
		Helper_Client::location("删除成功", "index.php?a=manage.manageclassifylist&manage_type={$manage_type}");
	}
	/** 下载**/
	public function actionoutput(){
		$fname = $this->get->manage_file;
		$fpath = UPLOAD_PATH."/manage_file/{$fname}";

		// 输出下载文件头s
		header("Content-Disposition: attachment; filename={$fname}");
		// 在线输出
		readfile($fpath);

	}

}

?>